package org.jeecg.modules.shop.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.system.vo.Role;
import org.jeecg.common.util.oConvertUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.shop.entity.RsProduct;
import org.jeecg.modules.shop.entity.RsSelectProduct;
import org.jeecg.modules.shop.service.IRsProductService;
import org.jeecg.modules.shop.service.IRsSelectProductService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 选品表
 * @Author: jeecg-boot
 * @Date:   2020-08-07
 * @Version: V1.0
 */
@Api(tags="选品表")
@RestController
@RequestMapping("/shop/rsSelectProduct")
@Slf4j
public class RsSelectProductController extends JeecgController<RsSelectProduct, IRsSelectProductService> {
	 @Autowired
	 private IRsSelectProductService rsSelectProductService;

	@Resource
	private IRsProductService rsProductService;
	 @Resource
	 private ISysBaseAPI sysBaseAPI;

	 /**
	  * 分页列表查询
	  *
	  * @param rsSelectProduct
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "选品表-分页列表查询")
	 @ApiOperation(value = "选品表-分页列表查询", notes = "选品表-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<?> queryPageList(RsSelectProduct rsSelectProduct,
									@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
									@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
									HttpServletRequest req) {
		 LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
		 List<String> list = sysBaseAPI.getRoleIdsByUsername(sysUser.getUsername());
		 String roleId = list.get(0);
		 String roleCode = sysBaseAPI.getOne(roleId).getRoleCode();
		 if ("1".equals(roleCode)) {//选品
			 rsSelectProduct.setSelectUser(sysUser.getId());
		 }
		 if ("2".equals(roleCode)) {//设计,分给设计的产品
			 rsSelectProduct.setDesigner(sysUser.getId());
		 }
		 if ("3".equals(roleCode)) {//运营，选品提交的商品
			 rsSelectProduct.setSelectStatus("1");
		 }
		 QueryWrapper<RsSelectProduct> queryWrapper = QueryGenerator.initQueryWrapper(rsSelectProduct, req.getParameterMap());
		 Page<RsSelectProduct> page = new Page<RsSelectProduct>(pageNo, pageSize);
		 IPage<RsSelectProduct> pageList = rsSelectProductService.page(page, queryWrapper);
		 List<RsSelectProduct> pList = new ArrayList<>();
		 if(pageList!=null&&pageList.getSize()>0){
		 	for(RsSelectProduct entity :pageList.getRecords()){
		 		if("0".equals(entity.getSelectStatus())){
					entity.setSelectStatus("待提交");
				}
				if("1".equals(entity.getSelectStatus())){
					entity.setSelectStatus("已提交");
				}
				if("0".equals(entity.getAllocationStatus())){
					entity.setAllocationStatus("待分配");
				}
				if("1".equals(entity.getAllocationStatus())){
					entity.setAllocationStatus("已分配");
				}
				if("0".equals(entity.getDesignStatus())){
					entity.setDesignStatus("待提交");
				}
				if("1".equals(entity.getDesignStatus())){
					entity.setDesignStatus("已提交");
				}
				if(StringUtils.isNotBlank(entity.getDesigner())){
					entity.setDesigner(sysBaseAPI.getUserById(entity.getDesigner()).getUsername());
				}
				pList.add(entity);
				pageList.setRecords(pList);
			}
		 }
		 return Result.ok(pageList);
	 }

	 /**
	  * 添加
	  *
	  * @param rsSelectProduct
	  * @return
	  */
	 @AutoLog(value = "选品表-添加")
	 @ApiOperation(value = "选品表-添加", notes = "选品表-添加")
	 @PostMapping(value = "/add")
	 public Result<?> add(@RequestBody RsSelectProduct rsSelectProduct) {
		 rsSelectProduct.setSelectStatus("0");
		 rsSelectProduct.setAllocationStatus("0");
		 rsSelectProduct.setDesignStatus("0");
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 rsSelectProduct.setSelectUser(sysUser.getId());
		 rsSelectProductService.save(rsSelectProduct);
		 return Result.ok("添加成功！");
	 }

	 /**
	  * 编辑
	  *
	  * @param rsSelectProduct
	  * @return
	  */
	 @AutoLog(value = "选品表-编辑")
	 @ApiOperation(value = "选品表-编辑", notes = "选品表-编辑")
	 @PutMapping(value = "/edit")
	 public Result<?> edit(@RequestBody RsSelectProduct rsSelectProduct) {

		 rsSelectProductService.updateById(rsSelectProduct);
		 return Result.ok("编辑成功!");
	 }

	 /**
	  * 通过id删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "选品表-通过id删除")
	 @ApiOperation(value = "选品表-通过id删除", notes = "选品表-通过id删除")
	 @DeleteMapping(value = "/delete")
	 public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		 rsSelectProductService.removeById(id);
		 return Result.ok("删除成功!");
	 }

	 /**
	  * 批量删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "选品表-批量删除")
	 @ApiOperation(value = "选品表-批量删除", notes = "选品表-批量删除")
	 @DeleteMapping(value = "/deleteBatch")
	 public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		 this.rsSelectProductService.removeByIds(Arrays.asList(ids.split(",")));
		 return Result.ok("批量删除成功!");
	 }

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "选品表-通过id查询")
	 @ApiOperation(value = "选品表-通过id查询", notes = "选品表-通过id查询")
	 @GetMapping(value = "/queryById")
	 public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		 RsSelectProduct rsSelectProduct = rsSelectProductService.getById(id);
		 if (rsSelectProduct == null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.ok(rsSelectProduct);
	 }

	 /**
	  * 导出excel
	  *
	  * @param request
	  * @param rsSelectProduct
	  */
	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(HttpServletRequest request, RsSelectProduct rsSelectProduct) {
		 return super.exportXls(request, rsSelectProduct, RsSelectProduct.class, "选品表");
	 }

	 /**
	  * 通过excel导入数据
	  *
	  * @param request
	  * @param response
	  * @return
	  */
	 @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	 public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		 return super.importExcel(request, response, RsSelectProduct.class);
	 }

	 @AutoLog(value = "选品表-通过id提交")
	 @ApiOperation(value = "选品表-通过id提交", notes = "选品表-通过id提交")
	 @PostMapping(value = "/selectStatus")
	 public Result<?> selectStatus(@RequestBody Object[] ids) {
		 RsSelectProduct rsSelectProduct = new RsSelectProduct();
		 for (int i = 0; i < ids.length; i++) {
			 rsSelectProduct = rsSelectProductService.getById(ids[i].toString());
			 if (rsSelectProduct == null) {
				 return Result.error("未找到对应数据");
			 } else {
				 rsSelectProduct.setSelectStatus("1");
				 rsSelectProductService.updateById(rsSelectProduct);
			 }
		 }
		 return Result.ok(rsSelectProduct);
	 }

	 @AutoLog("选品表-设计提交信息")
	 @ApiOperation(value = "选品表-设计提交信息",notes = "选品表-提交信息")
	 @PostMapping(value = "/insertStatus")
	 public Result<?> insertStatus(@RequestBody Object[] ids){
		 RsSelectProduct rsSelectProduct = new RsSelectProduct();
		 for (int i = 0; i < ids.length; i++) {
			 rsSelectProduct = rsSelectProductService.getById(ids[i].toString());
			 if (rsSelectProduct == null) {
				 return Result.error("未找到对应数据");
			 } else {
				 rsSelectProduct.setDesignStatus("1");
				 rsSelectProductService.updateById(rsSelectProduct);
			 }
		 }
		 return Result.ok(rsSelectProduct);
	 }
	 /**
	  * 运营分配选品给设计
	  */
	 @AutoLog(value = "选品表-分配")
	 @ApiOperation(value = "选品表-分配", notes = "选品表-分配")
	 @PutMapping(value = "/allocate")
	 public Result<?> allocate(@RequestBody Object[] ids, String designer) {
		 for (int i = 0; i < ids.length; i++) {
			 RsSelectProduct rsSelectProduct = rsSelectProductService.getById(ids[i].toString());
			 rsSelectProduct.setDesigner(designer);
			 rsSelectProduct.setAllocationStatus("1");
			 rsSelectProductService.updateById(rsSelectProduct);
		 }
		 return Result.ok("分配成功!");
	 }
	 /**
	  * 设计师列表
	  */
	 @AutoLog(value = "选品分配-设计师列表查询")
	 @ApiOperation(value = "选品分配-设计师列表查询", notes = "选品分配-设计师列表查询")
	 @GetMapping(value = "/getUserList")
	 public Result<?> getUserList(String roleCode) {
		 List<LoginUser> list = sysBaseAPI.getUserList(roleCode);
		 return Result.ok(list);
	 }
 }

